<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

$army_id = getPostValue( 'army_id' );
$query = "SELECT ruler_id, name, training, x, y, mission " .
		"FROM army " .
		"WHERE army_id = '" . $army_id . "'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	if( $row[ 'training' ] > 0 ) {
		$name = $row[ 'name' ];
		$status = translate( "This army can't transfer right now." );
	}
	else if( $row[ 'mission' ] > 0 ) {
		$status = translate( "This army can't transfer because it is on a mission." );
	}
	else {
		$status = '';
		if( allow_transfer( $row[ 'x' ], $row[ 'y' ] ) ) {
			$name = $row[ 'name' ];
			mysql_free_result( $result );
			if( $_POST[ 'from_scouts' ] != "" && $_POST[ 'from_scouts' ] > 0 ) {
				$from_scout = getPostValue( 'from_scouts' );
			}
			else {
				$from_scout = 0;
			}
			if( $_POST[ 'from_infantry' ] != "" && $_POST[ 'from_infantry' ] > 0 ) {
				$from_infantry = getPostValue( 'from_infantry' );
			}
			else {
				$from_infantry = 0;
			}
			if( isset( $_POST[ 'from_cavalry' ] ) && $_POST[ 'from_cavalry' ] > 0 ) {
				$from_cavalry = getPostValue( 'from_cavalry' );
			}
			else {
				$from_cavalry = 0;
			}
			if( isset( $_POST[ 'from_artillery' ] ) && $_POST[ 'from_artillery' ] > 0 ) {
				$from_artillery = getPostValue( 'from_artillery' );
			}
			else {
				$from_artillery = 0;
			}
			
			if( $_POST[ 'to_scouts' ] != "" && $_POST[ 'to_scouts' ] > 0 ) {
				$to_scout = getPostValue( 'to_scouts' );
			}
			else {
				$to_scout = 0;
			}
			if( $_POST[ 'to_infantry' ] != "" && $_POST[ 'to_infantry' ] > 0 ) {
				$to_infantry = getPostValue( 'to_infantry' );
			}
			else {
				$to_infantry = 0;
			}
			if( isset( $_POST[ 'to_cavalry' ] ) && $_POST[ 'to_cavalry' ] > 0 ) {
				$to_cavalry = getPostValue( 'to_cavalry' );
			}
			else {
				$to_cavalry = 0;
			}
			if( isset( $_POST[ 'to_artillery' ] ) && $_POST[ 'to_artillery' ] > 0 ) {
				$to_artillery = getPostValue( 'to_artillery' );
			}
			else {
				$to_artillery = 0;
			}
			
			if( getPostValue( 'add_all' ) == 'add_all'  ) {
				$query = "SELECT scouts, infantry, cavalry, artillery " .
						"FROM ruler " .
						"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				$to_scout = $row[ 'scouts' ];
				$to_infantry = $row[ 'infantry' ];
				$to_cavalry = $row[ 'cavalry' ];
				$to_artillery = $row[ 'artillery' ];
			}
			elseif( getPostValue( 'remove_all' ) == 'remove_all' ) {
				$query = "SELECT scout, infantry, cavalry, artillery " .
						"FROM army " .
						"WHERE army_id = " . $army_id;
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array($result, MYSQL_ASSOC);
				$from_scout = $row[ 'scout' ];
				$from_infantry = $row[ 'infantry' ];
				$from_cavalry = $row[ 'cavalry' ];
				$from_artillery = $row[ 'artillery' ];
			}
			
			$_SESSION[ 'transfer_from_scouts' ] = $from_scout;
			$_SESSION[ 'transfer_from_infantry' ] = $from_infantry;
			$_SESSION[ 'transfer_from_cavalry' ] = $from_cavalry;
			$_SESSION[ 'transfer_from_artillery' ] = $from_artillery;
			$_SESSION[ 'transfer_to_scouts' ] = $to_scout;
			$_SESSION[ 'transfer_to_infantry' ] = $to_infantry;
			$_SESSION[ 'transfer_to_cavalry' ] = $to_cavalry;
			$_SESSION[ 'transfer_to_artillery' ] = $to_artillery;
			
			if( $from_scout + $from_infantry + $from_cavalry + $from_artillery > 0 
				&& $to_scout + $to_infantry + $to_cavalry + $to_artillery > 0 ) {
				$status = translate( "You can't add and remove soldiers from an army at the same time." );
			}
			elseif ( $from_scout + $from_infantry + $from_cavalry + $from_artillery > 0 ) { // remove soldiers from this army
				$query = "SELECT scout, infantry, cavalry, artillery FROM army WHERE army_id = " . $army_id;
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				
				if( $from_scout > $row[ 'scout' ] ) {
					$status = translate( "You don't have" ) . " " . number_format( $from_scout ) . " " . translate( "scouts in this army." );
				}
				else if( $from_infantry > $row[ 'infantry' ] ) {
					$status = translate( "You don't have" ) . " " . number_format( $from_infantry ) . " " . translate( "infantry in this army." );
				}
				else if( $from_cavalry > $row[ 'cavalry' ] ) {
					$status = translate( "You don't have" ) . " " . number_format( $from_cavalry ) . " " . translate( "cavalry in this army." );
				}
				else if( $from_artillery > $row[ 'artillery' ] ) {
					$status = translate( "You don't have" ) . " " . number_format( $from_artillery ) . " " . translate( "artillery in this army." );
				}
				else {
					$current_turn = getCurrentTurn();
					$query = "INSERT INTO transfer ( scouts, infantry, cavalry, artillery, from_army_id, complete_turn, ruler_id ) " .
							"VALUES ( $from_scout, $from_infantry, $from_cavalry, $from_artillery, $army_id, " . ( $current_turn + 8 ) . ", " . $_SESSION[ 'ruler_id' ] . " )";
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	
					/*$query = "UPDATE army SET scout = " . ( $row[ 'scout' ] - $from_scout ) . ", " .
							"infantry =  " . ( $row[ 'infantry' ] - $from_infantry ) . ", " .
							"cavalry = " . ( $row[ 'cavalry' ] - $from_cavalry ) . ", " .
							"artillery = " . ( $row[ 'artillery' ] - $from_artillery ) . " " .
							"WHERE army_id = " . $army_id;
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );*/
	
					$query = "UPDATE army SET training = 1 WHERE army_id = " . $army_id;
					mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				}
			}
			elseif ( $to_scout + $to_infantry + $to_cavalry + $to_artillery > 0 ) {
				$query = "SELECT * FROM army WHERE army_id = " . $army_id;
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		
				$size = $row[ 'scout' ] + $row[ 'infantry' ] + $row[ 'cavalry' ] + $row[ 'artillery' ];
		
				$query = "SELECT rank FROM officers WHERE officer_id = " . $row[ 'officer_id' ];
				$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		
				if( isset( $row2[ 'rank' ] ) ) {
					$maxsize = 50;
					for( $i = 0; $i < $row2[ 'rank' ]; $i ++ ) {
						$maxsize *= 5;
					}
				}
				else {
					$maxsize = 10;
				}
				
				$addedStatus = increaseRankOfficer( $row[ 'officer_id' ], ( $size + $to_scout + $to_infantry + $to_cavalry + $to_artillery ) );
				if( $addedStatus != '' ) {
					$addedStatus = '<br />' . translate( $addedStatus );
				}
				
				if( $size >= $maxsize ) {
					$status = translate( "This army is already at maximum size!" . $addedStatus );
				}
				else {
					if( $size + $to_scout + $to_infantry + $to_cavalry + $to_artillery > $maxsize ) {
						$status = translate( "Can't add all units to the army, maximum sized exceeded." . $addedStatus );
					}
					else {
						$status = translate( "Soldiers are joining up with the army." );
					
						$query = "SELECT scouts, infantry, cavalry, artillery FROM ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
						$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						$row = mysql_fetch_array( $result, MYSQL_ASSOC );
						
						if( $to_scout > $row[ 'scouts' ] ) {
							$status = translate( "You don't have" ) . " " . number_format( $to_scout ) . " " . translate( "scouts." );
						}
						elseif( $to_infantry > $row[ 'infantry' ] ) {
							$status = translate( "You don't have" ) . " " . number_format( $to_infantry ) . " " . translate( "infantry." );
						}
						elseif( $to_cavalry > $row[ 'cavalry' ] ) {
							$status = translate( "You don't have" ) . " " . number_format( $to_cavalry ) . " " . translate( "cavalry." );
						}
						elseif( $to_artillery > $row[ 'artillery' ] ) {
							$status = translate( "You don't have" ) . " " . number_format( $to_artillery ) . " " . translate( "artillery." );
						}
						else {
							$current_turn = getCurrentTurn();
							$query = "INSERT INTO transfer ( scouts, infantry, cavalry, artillery, to_army_id, complete_turn, ruler_id ) " .
									"VALUES ( $to_scout, $to_infantry, $to_cavalry, $to_artillery, $army_id, " . ( $current_turn + 16 ) . ", " . $_SESSION[ 'ruler_id' ] . " )";
							mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			
							/*$query = "UPDATE ruler SET scouts = " . ( $row[ 'scouts' ] - $to_scout ) . ", " .
									"infantry =  " . ( $row[ 'infantry' ] - $to_infantry ) . ", " .
									"cavalry = " . ( $row[ 'cavalry' ] - $to_cavalry ) . ", " .
									"artillery = " . ( $row[ 'artillery' ] - $to_artillery ) . " " .
									"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ];
							mysql_query( $query ) or die( "Query failed : " . mysql_error() );*/
			
							$query = "UPDATE army SET training = 1 WHERE army_id = " . $army_id;
							mysql_query( $query ) or die( "Query failed : " . mysql_error() );
						}
					}
				}
			}
			else {
				$status = translate( "Nothing set to add or remove." );
			}
		}
	}

	$_SESSION[ 'add_army_status' ] = $status;
	header( "Location: army.php?name=$name" );
}
else {
	?>
	<HTML>
	<HEAD>
	<TITLE>Ages of Strife: Access denied</TITLE>
	<link rel="stylesheet" href="../global/agesofstrife.css">
	</HEAD>
	<BODY>
	<div id="container">
	<?php
	include( "../global/tabs.php" );
	?>
	<div id="main-content">
	You don't own this army!<br />
	</div>
	</div>
	</BODY>
	</HTML>
	<?php
}
?>
